package com.hdoit.ares.logical.acc.dao.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.alibaba.druid.util.StringUtils;
import com.hdoit.ares.logical.acc.dao.IGoldenBeanDetailDao;
import com.hdoit.ares.logical.acc.mapper.GoldenBeanDetailMapper;
import com.hdoit.ares.logical.acc.model.GoldenBeanDetail;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.viewmodel.acc.FindGoldenDetail;
import com.hdoit.ares.logical.viewmodel.acc.GoldenBeanDetailSearch;
import com.hdoit.ares.selfcommon.enums.acc.HesitatePeriodState;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.message.DaoBaseException;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      caochengmeng
 * @email       
 * @createtime  2017年11月29日 19:59:46
 * @comment
 */
@Component
public class GoldenBeanDetailDaoImpl extends SuperDao<GoldenBeanDetail> implements IGoldenBeanDetailDao{

	private GoldenBeanDetailMapper goldenBeanDetailMapper;
	public GoldenBeanDetailMapper getGoldenBeanDetailMapper() {
		return goldenBeanDetailMapper;
	}
	@Resource
	public void setGoldenBeanDetailMapper(GoldenBeanDetailMapper goldenBeanDetailMapper) {
		this.goldenBeanDetailMapper = goldenBeanDetailMapper;
	}

	//后台按条件查询
	@Override
	public List<GoldenBeanDetail> queryList(GoldenBeanDetailSearch search, PagerModel pagerModel) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.select("goldenBeanDetail.*","customer.userIdentity as userIdentity","customer.username as customerName")
						.from(GoldenBeanDetail.class,"goldenBeanDetail")
						.leftJoin(Customer.class, "customer")
						.on("customer.id","goldenBeanDetail.customerId")
						.setMainTableAlias("goldenBeanDetail");
		
		appendHoutai(search,finderExpression);
		
		    if(pagerModel == null){
	            return queryList(finderExpression, search); 
	          }
	        return queryList(finderExpression, search, pagerModel);
	}

	@Override
	public List<GoldenBeanDetail> queryList(GoldenBeanDetailSearch search) {
		return queryList(search,null);
	}

	//后台按条件查询数量
	@Override
	public int queryCount(GoldenBeanDetailSearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.select("count(goldenBeanDetail.id) as count")
						.from(GoldenBeanDetail.class,"goldenBeanDetail")
						.leftJoin(Customer.class, "customer")
						.on("customer.id","goldenBeanDetail.customerId")
						.setMainTableAlias("goldenBeanDetail");
		
		appendHoutai(search,finderExpression);
		
		return queryCount(finderExpression, search);
	}
	
	@Override
	public GoldenBeanDetail queryModel(String key) throws DaoBaseException {
        IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.select("goldenBeanDetail.*","customer.userIdentity as userIdentity","customer.username as customerName")
						.from(GoldenBeanDetail.class,"goldenBeanDetail")
						.leftJoin(Customer.class, "customer")
						.on("customer.id","goldenBeanDetail.customerId")
						.andWhere("goldenBeanDetail.id = #{key}")
						.setParam("key", key)
						.setMainTableAlias("goldenBeanDetail");
		
		return queryModel(finderExpression);
	}

	@Override
	public List<GoldenBeanDetail> queryBonusList(GoldenBeanDetailSearch search, String id) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("goldenBeanDetail.*")
						.from(GoldenBeanDetail.class,"goldenBeanDetail")
						.andEq("customerId",id);
	    
		return queryList(finderExpression);
	}

	//金豆详情的总收入
	@Override
	public GoldenBeanDetail querySumIncomeMoney(FindGoldenDetail findGoldenDetail,String id) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("sum(goldenBeanAccount) as incomeMoney")
		                .from(GoldenBeanDetail.class)
		                .andEq("customerId", id)
		                .andEq("state","income");
		
		appendFinder(findGoldenDetail,finderExpression);
		
		return queryModel(finderExpression);
	}

	//金豆详情的总支出
	@Override
	public GoldenBeanDetail querySumExpendMoney(FindGoldenDetail findGoldenDetail,String id) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("sum(goldenBeanAccount) as expendMoney")
		                .from(GoldenBeanDetail.class)
		                .andEq("customerId", id)
		                .andEq("state","pay");
		
		appendFinder(findGoldenDetail,finderExpression);
		
		return queryModel(finderExpression);
	}

	@Override
	public List<GoldenBeanDetail> queryGoldenBeanDetail(FindGoldenDetail findGoldenDetail,String custId) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(GoldenBeanDetail.class)
						.andEq("customerId", custId);
		
		appendFinder(findGoldenDetail,finderExpression);

		return queryList(finderExpression);
	}
	
	//金豆详情的高级查询
	private void appendFinder(FindGoldenDetail findGoldenDetail,IFinderExpression finderExpression){
	
		//交易类型
		if(!StringUtils.isEmpty(findGoldenDetail.getGoldenBeanType())){
		finderExpression.andEq("goldenBeanType", findGoldenDetail.getGoldenBeanType());
	    }
	
	if(!StringUtils.isEmpty(findGoldenDetail.getOrder())){
		if("desc".equals(findGoldenDetail.getUpOrDown())){
			finderExpression.addOrder(findGoldenDetail.getOrder(), OrderEnum.desc);
		}
		if("asc".equals(findGoldenDetail.getUpOrDown())){
			finderExpression.addOrder(findGoldenDetail.getOrder(), OrderEnum.asc);
		}
	}
	//开始时间
	if(findGoldenDetail.getStartDate()!=null){
		finderExpression.andWhere("TIMESTAMPDIFF(SECOND,#{startDate},time)>=0")
		  .setParam("startDate", findGoldenDetail.getStartDate());
	}
	//结束时间
	if(findGoldenDetail.getEndDate()!=null){
		finderExpression.andWhere("TIMESTAMPDIFF(SECOND,#{endDate},time)<=0")
		  .setParam("endDate", findGoldenDetail.getEndDate());
	}
}
	
	public Boolean updateHesitatePeriodState(Date lastDate) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		
		finderExpression.update(GoldenBeanDetail.class)
						.set("hesitatePeriodState=#{hesitatePeriodState}")
						.andWhere("TIMESTAMPDIFF(DAY,time,#{lastDate})>=hesitatePeriod")
						.andWhere("hesitatePeriodState=#{oldState}")
						.setParam("oldState", HesitatePeriodState.Hesitating)
						.setParam("hesitatePeriodState",HesitatePeriodState.Hesitated)
						.setParam("lastDate", lastDate);
		
		return update(finderExpression) > 0;
	}
	
	
	@Override
	public GoldenBeanDetail queryGoldenInHesitate(String id) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("SUM(`goldenBeanAccount`) as sumgolden")
		                .from(GoldenBeanDetail.class)
		                .andEq("customerId", id)
		                .andEq("state","income")
		                .andEq("hesitatePeriodState","hesitating");
		
		return queryModel(finderExpression);

	}


	@Override
	public GoldenBeanDetail queryGoldenBean(String id) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("*")
		                .from(GoldenBeanDetail.class)
		                .andEq("id", id);
		
		return queryModel(finderExpression);
	}

	    //后台的高级搜索条件
		private void appendHoutai(GoldenBeanDetailSearch search, IFinderExpression finder) {
			
			if(search.getStartDate()!=null){
				finder.andWhere("TIMESTAMPDIFF(SECOND,#{startDate},goldenBeanDetail.time)>=0")
					  .setParam("startDate", search.getStartDate());
			}
			if(search.getEndDate()!=null){
				finder.andWhere("TIMESTAMPDIFF(SECOND,#{endDate},goldenBeanDetail.time)<=0")
					  .setParam("endDate", search.getEndDate());
			}
			
			if(!StringUtils.isEmpty(search.getUserIdentity())){
				finder.andWhere("customer.userIdentity like #{userIdentity}")
				  	  .setParam("userIdentity","%"+search.getUserIdentity()+"%");
			}
			if(!StringUtils.isEmpty(search.getCustomerName())){
				finder.andWhere("customer.userName like #{customerName}")
					  .setParam("customerName","%"+search.getCustomerName()+"%");
			}
			if(!StringUtils.isEmpty(search.getOutTradeNo())){
				finder.andWhere("goldenBeanDetail.outTradeNo like #{outTradeNo}")
				  	  .setParam("outTradeNo","%"+search.getOutTradeNo()+"%");
			}		
			if(!StringUtils.isEmpty(search.getGoldenBeanType())){
				finder.andWhere("goldenBeanDetail.goldenBeanType = #{goldenBeanType}")
					  .setParam("goldenBeanType", search.getGoldenBeanType());
			}		
		}

		//分页查询金豆详情列表 
		@Override
		public List<GoldenBeanDetail> queryGoldenList(FindGoldenDetail findGoldenDetail, String custId, Integer start,Integer length) {
			IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
			finderExpression.select("*")
							.from(GoldenBeanDetail.class)
							.andEq("customerId", custId)
							.limit(start, length);
			
			appendFinder(findGoldenDetail,finderExpression);

			return queryList(finderExpression);
		}

		@Override
		public GoldenBeanDetail queryModelBySearch(GoldenBeanDetailSearch search) {
			IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
			finderExpression.select("*")
							.from(GoldenBeanDetail.class);
							
			
			findModelBySearch(search,finderExpression);

			return queryModel(finderExpression);
		}

		private void findModelBySearch(GoldenBeanDetailSearch search,
				IFinderExpression finderExpression) {
			if(!StringUtils.isEmpty(search.getCustomerId())){
				finderExpression.andWhere("customerId = #{customerId}")
					  			.setParam("customerId", search.getCustomerId());
			}
			if(!StringUtils.isEmpty(search.getGoldenBeanType())){
				finderExpression.andWhere("goldenBeanType = #{goldenBeanType}")
					  			.setParam("goldenBeanType", search.getGoldenBeanType());
			}
			
			finderExpression.andWhere("TO_DAYS(`time`) = TO_DAYS(NOW())");
		}

		/*//查询当前页面的总收入
		@Override
		public GoldenBeanDetail querySumIncome(FindGoldenDetail findGoldenDetail, String id, Integer start,Integer length) {
			
			IFinderExpression oneChildIdFinder=FinderExpressionFactory.createExpression();
			oneChildIdFinder.select("*")
							.from(GoldenBeanDetail.class)
							.andWhere("customerId=#{id}")
							.limit(start, length);
			
			IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
			finderExpression.select("sum(goldenBeanAccount) as incomeMoney")
			                .from("("+oneChildIdFinder.toSql()+") as nowTable")
			                .andEq("state","income")
			                .setParam("id", id);
			
			appendFinder(findGoldenDetail,finderExpression);
			
			return queryModel(finderExpression);
		}

		//查询当前页面的总支出
		@Override
		public GoldenBeanDetail querySumExpend(FindGoldenDetail findGoldenDetail, String id, Integer start,Integer length) {
			
			IFinderExpression oneChildIdFinder=FinderExpressionFactory.createExpression();
			oneChildIdFinder.select("*")
			                .from(GoldenBeanDetail.class)
			                .andWhere("customerId=#{id}")
			                .limit(start, length);
			
			IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
			finderExpression.select("sum(goldenBeanAccount) as expendMoney")
			                .from("("+oneChildIdFinder.toSql()+") as nowTable")
			                .andEq("state","pay")
			                .setParam("id", id);
			
			appendFinder(findGoldenDetail,finderExpression);
			
			return queryModel(finderExpression);
		}*/

}
